﻿using ConfExpert.Enums;
using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraPivotGrid;
using Extensions;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ITaskManager.Forms.IT
{
    public partial class FaturaPivot : XForm
    {
        public FaturaPivot()
        {
            InitializeComponent();
        }

        private void FaturaPivot_Load(object sender, EventArgs e)
        {            
            RefreshData();
            ExpandCollapse(splitContainerControl1, AcKapa.Collapse);
            //btnExport.ItemClick += btnExport_ItemClick;

            pvg.OptionsPrint.PrintColumnHeaders = DevExpress.Utils.DefaultBoolean.False;
            pvg.OptionsPrint.PrintDataHeaders = DevExpress.Utils.DefaultBoolean.False;
            pvg.OptionsPrint.PrintFilterHeaders = DevExpress.Utils.DefaultBoolean.False;
            pvg.OptionsPrint.MergeRowFieldValues = false;
            pvg.OptionsPrint.MergeColumnFieldValues = false;
            chkEskiFaturalar.CheckedChanged += chkEskiFaturalar_CheckedChanged;

            splitContainerControl1.AutoSize();
            btnKaydet.ItemClick += btnKaydet_ItemClick;
            btnKaydet.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            cmbGrouping.EditValueChanged += cmbGrouping_EditValueChanged;
        }

        void cmbGrouping_EditValueChanged(object sender, EventArgs e)
        {
            InitPivot(pvg, bs);
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEdit();
            gridView1.Update("IT_FaturaLinesId,UrunAdi,KullanimAmaci,Adet,BirimFiyat", "IT_FaturaLines");
            gridView1.BestFitColumns();
        }

        void chkEskiFaturalar_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshData();
        }

        void btnExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ExportPivot(pvg);
        }

        BindingSource bs;
        void pivotGridControl1_CustomAppearance(object sender, PivotCustomAppearanceEventArgs e)
        {
            if (e.Focused)
                e.Appearance.BackColor = e.Appearance.BackColor2 = Color.YellowGreen;   // Color.FromArgb(255, 192, 0);
        }


        void RefreshData()
        {
            bs = gridView1.Init("IT_GetFaturaPivot", /*Session.SirketId*/0, chkEskiFaturalar.Checked);
            gridView1.SutunEkle("ToplamFiyat", typeof(decimal), "Adet*BirimFiyat");
            gridView1.FormatAsNumber(1, "BirimFiyat", "ToplamFiyat");
            gridView1.AddSummary("ToplamFiyat", "Adet");
            gridView1.MakeReadOnly("UrunAdi", "KullanimAmaci", "Adet", "BirimFiyat");

            InitPivot(pvg, bs);

            pvg.Cells.Selection = new Rectangle(new Point(0, 0), new Size(1, 1));
            pvg.BestFit();
        }

        bool PivotInitialized = false;
        void InitPivot(PivotGridControl pvg, BindingSource bs)
        {
            pvg.BeginUpdate();

            pvg.DataSource = null;
            pvg.DataSource = bs;

            if (!PivotInitialized)
            {
                pvg.OptionsCustomization.CustomizationFormStyle = DevExpress.XtraPivotGrid.Customization.CustomizationFormStyle.Excel2007;
                pvg.CellClick += new PivotCellEventHandler(pivotGridControl1_CellClick);
                pvg.CellSelectionChanged += new EventHandler(pvg_CellSelectionChanged);
                pvg.FocusedCellChanged += new EventHandler(pvg_FocusedCellChanged);
                pvg.FieldValueDisplayText += new PivotFieldDisplayTextEventHandler(pivotGrid_FieldValueDisplayText);
                pvg.CustomAppearance += new PivotCustomAppearanceEventHandler(pivotGridControl1_CustomAppearance);

                //AddNewField(pvg, "UrunAdi", PivotArea.FilterArea);
                AddNewField(pvg, "TedarikciId", PivotArea.FilterArea).FieldEdit = RI(Rp.Tedarikci);
                AddNewField(pvg, "SirketId", PivotArea.FilterArea).FieldEdit = RI(Rp.Sirket);

                AddNewField(pvg, "IT_FaturaTipiId", PivotArea.ColumnArea).FieldEdit = RI(Rp.IT_FaturaTipi);
                AddNewField(pvg, "ToplamFiyat", PivotArea.DataArea);

                foreach (PivotGridField pgf in pvg.Fields)
                {
                    pgf.SortOrder = PivotSortOrder.Ascending;
                    pgf.Options.AllowSort = DevExpress.Utils.DefaultBoolean.True;
                }
                PivotInitialized = true;
            }

            if (pvg.Fields["SirketId"] != null)
                pvg.Fields.Remove(pvg.Fields["SirketId"]);
            if (pvg.Fields["TedarikciId"] != null)
                pvg.Fields.Remove(pvg.Fields["TedarikciId"]);
            if (pvg.Fields["IT_FaturaTipiId"] != null)
                pvg.Fields.Remove(pvg.Fields["IT_FaturaTipiId"]);

            switch (cmbGrouping.EditValue.ToStr())
            {
                case "Tedarikçi":
                    AddNewField(pvg, "TedarikciId", PivotArea.RowArea).FieldEdit = RI(Rp.Tedarikci);
                    sel = pvg.Fields["TedarikciId"];
                    AddNewField(pvg, "IT_FaturaTipiId", PivotArea.ColumnArea).FieldEdit = RI(Rp.IT_FaturaTipi);
                    break;

                case "Şirket":
                    AddNewField(pvg, "SirketId", PivotArea.RowArea).FieldEdit = RI(Rp.Sirket);
                    sel = pvg.Fields["SirketId"];
                    AddNewField(pvg, "IT_FaturaTipiId", PivotArea.ColumnArea).FieldEdit = RI(Rp.IT_FaturaTipi);
                    break;

                case "Karma":
                    AddNewField(pvg, "TedarikciId", PivotArea.RowArea).FieldEdit = RI(Rp.Tedarikci);
                    sel = pvg.Fields["TedarikciId"];
                    AddNewField(pvg, "SirketId", PivotArea.ColumnArea).FieldEdit = RI(Rp.Sirket);
                    break;
            }

            pvg.EndUpdate();
            sel.BestFit();
        }

        PivotGridField sel;
        void pvg_FocusedCellChanged(object sender, EventArgs e)
        {
            CellChanged();
        }


        void pvg_CellSelectionChanged(object sender, EventArgs e)
        {
            CellChanged();
        }

        private void CellChanged()
        {
            double PivotSum = 0.0;

            if (pvg.Cells.MultiSelection.SelectedCells.Count > 0)
            {
                foreach (Point cell in pvg.Cells.MultiSelection.SelectedCells)
                {
                    DevExpress.XtraPivotGrid.PivotCellEventArgs cellInfo = pvg.Cells.GetCellInfo(cell.X, cell.Y);
                    if (cellInfo.ColumnValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value &&
                        cellInfo.RowValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value)
                        PivotSum += Convert.ToDouble(pvg.GetCellValue(cell.X, cell.Y));
                }
            }
            else
            {
                DevExpress.XtraPivotGrid.PivotCellEventArgs cellInfo = pvg.Cells.GetFocusedCellInfo();
                if (cellInfo.ColumnValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value &&
                    cellInfo.RowValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value)
                    PivotSum += Convert.ToDouble(pvg.GetCellValue(pvg.Cells.FocusedCell.X, pvg.Cells.FocusedCell.Y));

            }
            //lblPivot.Text = PivotSum == 0 ? string.Empty : String.Format("{0:#,##0.## mt'.'}", PivotSum);
        }

        void pivotGridControl1_CellClick(object sender, DevExpress.XtraPivotGrid.PivotCellEventArgs e)
        {
            gridControl1.DataSource = e.CreateDrillDownDataSource();
            ExpandCollapse(splitContainerControl1, (gridView1.RowCount > 0 ? AcKapa.Expand : AcKapa.Collapse));
            gridView1.BestFitColumns();
            gridView1.MoveFirst();        
        }
    }
}
